//
//  ISDUETHoangDaoViewController.m
//  12Cung
//
//  Created by Tran Bao Thai on 8/5/12.
//  Copyright (c) 2012 Tran Bao Thai. All rights reserved.
//

#import "ISDUETHoangDaoViewController.h"
#import "ISDUETHoangDaoDetailViewController.h"
#import "ReflectionView.h"
#import "Constants.h"

@interface ISDUETHoangDaoViewController ()

@end

@implementation ISDUETHoangDaoViewController
@synthesize tableData;
@synthesize carouselView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSString* pathToDictFile = [[NSBundle mainBundle] pathForResource:@"HoangDao" ofType:@"plist"];
        tableData = [[NSMutableArray alloc] initWithContentsOfFile:pathToDictFile];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    NSString* pathToDictFile = [[NSBundle mainBundle] pathForResource:@"HoangDao" ofType:@"plist"];
    tableData = [[NSMutableArray alloc] initWithContentsOfFile:pathToDictFile];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    carouselView.type = iCarouselTypeCoverFlow;
}

- (void)viewDidUnload
{
    [self setCarouselView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [tableData release];
    carouselView.delegate = nil;
    carouselView.dataSource = nil;
    [carouselView release];
    [super dealloc];
}

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return NUMBER_OF_ITEMS;
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return NUMBER_OF_VISIBLE_ITEMS;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
    UIButton* btnICarousel;
    UILabel* name = nil;
    UILabel* dateOfBirth = nil;
    //NSString* number = [[NSString alloc] initWithFormat:@"%u", index];
    
    ReflectionView* resultView = [[[ReflectionView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]autorelease];
    // setup content
    btnICarousel = [[[UIButton alloc]initWithFrame:CGRectMake(25.0f, 50.0f, 150.0f, 150.0f)]autorelease];
    
    name = [[[UILabel alloc]initWithFrame:CGRectMake(20.0f, 0.0f, 160.0f, 30.0f)]autorelease];
    name.textAlignment = UITextAlignmentCenter;
    name.font = [UIFont fontWithName:@"Marker Felt" size:18];
    name.backgroundColor = [UIColor clearColor];
    name.textColor = [UIColor whiteColor];
    
    dateOfBirth = [[[UILabel alloc]initWithFrame:CGRectMake(20.0f, 25.0f, 160.0f, 30.0f)]autorelease];
    dateOfBirth.textAlignment = UITextAlignmentCenter;
    dateOfBirth.font = [UIFont fontWithName:@"Marker Felt" size:18];
    dateOfBirth.backgroundColor = [UIColor clearColor];
    dateOfBirth.textColor = [UIColor whiteColor];
    
    [resultView addSubview:btnICarousel];
    [resultView addSubview:name];
    [resultView addSubview:dateOfBirth];
    
    [btnICarousel setImage:[UIImage imageNamed:[NSString stringWithFormat:@"btnICarousel%u.png", index]] forState:UIControlStateNormal];
    [btnICarousel addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [btnICarousel setTag:index];
    
    NSDictionary* itemDict = [tableData objectAtIndex:index];
    [name setText:[itemDict objectForKey:KEY_NAME]];
    [dateOfBirth setText:[itemDict objectForKey:KEY_DATE]];
    
    [resultView update];
    
    return resultView;
}

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index
{
    return nil;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
	//note: placeholder views are only displayed on some carousels if wrapping is disabled
	return 0;//INCLUDE_PLACEHOLDERS? 2: 0;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

- (CGFloat)carousel:(iCarousel *)carousel itemAlphaForOffset:(CGFloat)offset
{
	//set opacity based on distance from camera
    return 1.0f - fminf(fmaxf(offset, 0.0f), 1.0f);
}

- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carouselView.itemWidth);
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return NO;
}
#pragma mark -
#pragma mark Controls

- (void)buttonPressed:(UIButton *)sender{
    NSLog(@"TAG: %u", sender.tag);
    ISDUETHoangDaoDetailViewController* hdDetailVC = [[[ISDUETHoangDaoDetailViewController alloc]initWithNibName:@"ISDUETHoangDaoDetailViewController" bundle:nil]autorelease];
    hdDetailVC.detailDict = [tableData objectAtIndex:sender.tag];
    [self.navigationController pushViewController:hdDetailVC animated:YES];
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
